import XLSX from 'xlsx'
export default function Excel(headers, data, fileName = '导出结果.xlsx') {
  const _headers = headers
    .map((item, i) =>
      Object.assign(
        {},
        {
          key: item.key,
          title: item.title,
          position: String.fromCharCode(65 + i) + 1,
        }
      )
    )
    .reduce(
      (prev, next) =>
        Object.assign({}, prev, {
          [next.position]: { key: next.key, v: next.title },
        }),
      {}
    )
  const _data = data
    .map((item, i) =>
      headers.map((key, j) =>
        Object.assign(
          {},
          {
            content: item[key.key],
            position: String.fromCharCode(65 + j) + (i + 2),
          }
        )
      )
    )
    .reduce((prev, next) => prev.concat(next))
    .reduce(
      (prev, next) =>
        Object.assign({}, prev, { [next.position]: { v: next.content } }),
      {}
    )
  const output = Object.assign({}, _headers, _data)
  const outputPos = Object.keys(output)
  const ref = `${outputPos[0]}:${outputPos[outputPos.length - 1]}`
  const wb = {
    SheetNames: ['mySheet'],
    Sheets: {
      mySheet: Object.assign({}, output, {
        '!ref': ref,
        // eslint-disable-next-line
        '!cols': headers.map((item) => ({ wpx: 100 })),
      }),
    },
  }

  // 导出 Excel
  XLSX.writeFile(wb, fileName)
}
